var xyz = {
    setBtnStatus:function () {
        var row = $('#grid_xyz').datagrid('getSelected');
        var type = $("[name='xyzsflx_type']").val();
        if(type==0){
            if (row) { //被选择的应收信用证
                $("#btn_xyz_add").hide();
                $("#btn_xyz_xyzhk").hide();
                $("#btn_xyz_mod").show();
                $("#btn_xyz_del").show();
                $("#btn_xyz_xyzbx").show();
            } else {    //未被选择的应收票据
                $("#btn_xyz_add").hide();
                $("#btn_xyz_xyzhk").hide();
                $("#btn_xyz_xyzbx").hide();
                $("#btn_xyz_mod").hide();
                $("#btn_xyz_del").hide();
            }
        }else{
            if (row) {   //被选择的应付信用证
                $("#btn_xyz_add").show();
                $("#btn_xyz_xyzhk").show();
                $("#btn_xyz_xyzbx").hide();
                $("#btn_xyz_mod").show();
                $("#btn_xyz_del").show();
            } else {  //未被选择的应付票据
                $("#btn_xyz_add").show();
                $("#btn_xyz_xyzhk").hide();
                $("#btn_xyz_xyzbx").hide();
                $("#btn_xyz_mod").hide();
                $("#btn_xyz_del").hide();
            }
        }
    }
}

$(function () {
    $("#xyzgsId").combotree({
        loadFilter:function (rows) {
            return convert(rows);
        }
    });
    var sysDate = $("#xyz_sysDate").val();
    //表格初始加载
        if( $("[name='xyzsflx_type']").val()==0 || !$("[name='xyzsflx_type']").val()){
            $('#grid_xyz').datagrid({
                url:$.webPath() + "/zjgl/xyz/xyzList",
                toolbar:"#toolbar_xyz",
                fitColumns:true,
                singleSelect:false,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:25,align: 'center'},
                    {field:'xyzhm', title:'信用证号码', width:25,align: 'center'},
                    {field:'shzt', title:'审核状态', width:15,align: 'center',
                        formatter:function (value) {
                        if (value == '0')
                            return '未审核';
                        else
                            return '已审核';
                    }},
                    {field:'ysczyh', title:'出证银行', width:15,align: 'center'},
                    {field:'bzj', title:'保证金(元)', width:20,align: 'right',
                        formatter:function(value){
                            if(!value)
                                return "无";
                            else
                                return value;
                        }
                    },
                    {field:'dqrq', title:'到期日', width:30,align: 'center',
                        formatter:function(value){
                        var val = formatDate(sysDate,value,5);
                        return val
                    }},
                    {field:'dqje', title:'到期金额(元)', width:30,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'czr', title:'出证人', width:12,align: 'center'},
                    {field:'skr', title:'收款人', width:30,align: 'center'},
                    {field:'sfwffx', title:'付息方', width:30, align: 'center',
                        formatter:function(value){
                            if(value==0)
                                return '我方付息';
                            if(value==1)
                                return '卖方付息';
                        }
                    },
                    {field:'lxe', title:'利息额(元)', width:30,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
//                    {field:"sfdrlx" ,title:"是否导入利息",width:30,
//                        formatter:function (value) {
//                            if (value == 'ydr')
//                                return '已导入';
//                            if (value == 'wdr')
//                                return '未导入';
//                        }},
                    {field:"jkrq" ,title:"借款日期",width:20,align: 'center'},
                    {field:"kzf" ,title:"开证费(元)",width:20,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }},
                    {field:"bz" ,title:"备注",width:30,align: 'center'},
                    {field:"status" ,title:"状态",width:12,align: 'center',
                        formatter:function (value) {
                        if (value == 1)
                                return '可操作';
                        if (value == 2)
                            return '已还款';
                        if(value== 3)
                            return '已变现';
                        if(value== 4)
                                return '已使用';
                    }}]],
                onLoadSuccess:function(){
                    xyz.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onSelect:function(rowIndex, rowData){
                    xyz.setBtnStatus();
                },
                onCheck :function(){
                    xyz.setBtnStatus();
                }
            });
        } else{
            $('#grid_xyz').datagrid({
                url:$.webPath() + "/zjgl/xyz/xyzList",
                toolbar:"#toolbar_xyz",
                fitColumns:true,
                singleSelect:false,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:25,align: 'center'},
                    {field:'xyzhm', title:'信用证号码', width:25,align: 'center'},
                    {field:'yfczyh_qc', title:'出证行', width:15,align: 'center'},
                    {field:'dqje', title:'到期金额(元)', width:20,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'bzj', title:'保证金(元)', width:25,align: 'right',
                        formatter:function(value){
                            if(!value)
                               return "无";
                            else
                              return value;
                        }
                    },
                    {field:'dqrq', title:'到期日', width:30,align: 'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'jzdw_jc', title:'交证单位', width:12,align: 'center'},
                    {field:'sfwffx', title:'付息方', width:20,align: 'center',
                        formatter:function(value){
                            if(value==0)
                                return '我方付息';
                            if(value==1)
                                return '卖方付息';
                        }
                    },
                    {field:'lxe', title:'利息额(元)', width:15, align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:"sfdrlx" ,title:"是否导入利息",width:30,align: 'center',
                        formatter:function (value) {
                            if (value == 'ydr')
                                return '已导入';
                            if (value == 'wdr')
                                return '未导入';
                        }},
                    {field:"jkrq" ,title:"借款日期",width:20,align: 'center'},
                    {field:"kzf" ,title:"开证费(元)",width:20,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }},
                    {field:"bz" ,title:"备注",width:15,align: 'center'},
                    {field:"status" ,title:"状态",width:15,align: 'center',
                        formatter:function (value) {
                            if (value == 1)
                                return '可操作';
                            if (value == 2)
                                return '已还款';
                            if(value== 3)
                                return '已变现';
                            if(value== 4)
                                return '已使用';
                        }}]],
                onLoadSuccess:function(){
                    xyz.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onClickRow:function(){
                    xyz.setBtnStatus();
                },
                onCheck :function(){
                    xyz.setBtnStatus();
                }
            });
        }

    //当类型切换时切换页面
    $("[name='xyzsflx_type']").combobox({onSelect:function () {
        //先清空表格
        xyz_search();
        $('#grid_xyz').datagrid("reload");
        if( $("[name='xyzsflx_type']").val()==0 || !$("[name='xyzsflx_type']").val()){
            //todo
            $("#xyz_zt").combobox({
                data:[{
                    label: '可操作',
                    value: '1'
                },{
                    label: '已变现',
                    value: '3'
                }]
            });
        }else{
            $("#xyz_zt").combobox({
                data:[{
                    label: '可操作',
                    value: '1'
                },{
                    label: '已使用',
                    value: '4'
                },{
                    label: '已还款',
                    value: '2'
                }]
            });
        }
        if( $("[name='xyzsflx_type']").val()==0 || !$("[name='xyzsflx_type']").val()){
            $('#grid_xyz').datagrid({
                url:$.webPath() + "/zjgl/xyz/xyzList",
                toolbar:"#toolbar_xyz",
                fitColumns:true,
                singleSelect:false,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:25,align: 'center'},
                    {field:'xyzhm', title:'信用证号码', width:25,align: 'center'},
                    {field:'shzt', title:'审核状态', width:15, align: 'center',
                        formatter:function (value) {
                            if (value == '0')
                                return '未审核';
                            else
                                return '已审核';
                        }},
                    {field:'ysczyh', title:'出证银行', width:15,align: 'center'},
                    {field:'bzj', title:'保证金(元)', width:20, align: 'right',
                        formatter:function(value){
                            if(!value)
                                return "无";
                            else
                                return value;
                        }
                    },
                    {field:'dqrq', title:'到期日', width:30, align: 'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'dqje', title:'到期金额(元)', width:30,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'czr', title:'出证人', width:12,align: 'center'},
                    {field:'skr', title:'收款人', width:30,align: 'center'},
                    {field:'sfwffx', title:'付息方', width:30,align: 'center',
                        formatter:function(value){
                            if(value==0)
                                return '我方付息';
                            if(value==1)
                                return '卖方付息';
                        }
                    },
                    {field:'lxe', title:'利息额(元)', width:30, align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
//                    {field:"sfdrlx" ,title:"是否导入利息",width:30,
//                        formatter:function (value) {
//                            if (value == 'ydr')
//                                return '已导入';
//                            if (value == 'wdr')
//                                return '未导入';
//                        }},
                    {field:"jkrq" ,title:"借款日期",width:20,align: 'center'},
                    {field:"kzf" ,title:"开证费(元)",width:20, align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }},
                    {field:"bz" ,title:"备注",width:30,align: 'center'},
                    {field:"status" ,title:"状态",width:12,align: 'center',
                        formatter:function (value) {
                            if (value == 1)
                                return '可操作';
                            if (value == 2)
                                return '已还款';
                            if(value== 3)
                                return '已变现';
                            if(value== 4)
                                return '已使用';
                        }}]],
                onLoadSuccess:function(){
                    xyz.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onClickRow:function(rowIndex, rowData){
                    xyz.setBtnStatus();
                },
                onCheck :function(){
                    xyz.setBtnStatus();
                }
            });
        } else{
            $('#grid_xyz').datagrid({
                url:$.webPath() + "/zjgl/xyz/xyzList",
                toolbar:"#toolbar_xyz",
                fitColumns:true,
                singleSelect:false,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:25,align: 'center'},
                    {field:'xyzhm', title:'信用证号码', width:25,align: 'center'},
                    {field:'yfczyh_qc', title:'出证行', width:15,align: 'center'},
                    {field:'dqje', title:'到期金额(元)', width:20, align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'bzj', title:'保证金(元)', width:25,align: 'right',
                        formatter:function(value){
                            if(!value)
                                return "无";
                            else
                                return value;
                        }
                    },
                    {field:'dqrq', title:'到期日', width:30,align: 'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'jzdw_jc', title:'交证单位', width:12,align: 'center'},
                    {field:'sfwffx', title:'付息方', width:20,align: 'center',
                        formatter:function(value){
                            if(value==0)
                                return '我方付息';
                            if(value==1)
                                return '卖方付息';
                        }
                    },
                    {field:'lxe', title:'利息额(元)', width:15, align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:"sfdrlx" ,title:"是否导入利息",width:30,align: 'center',
                        formatter:function (value) {
                            if (value == 'ydr')
                                return '已导入';
                            if (value == 'wdr')
                                return '未导入';
                        }},
                    {field:"jkrq" ,title:"借款日期",width:20,align: 'center'},
                    {field:"kzf" ,title:"开证费(元)",width:20,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }},
                    {field:"bz" ,title:"备注",width:15,align: 'center'},
                    {field:"status" ,title:"状态",width:15,align: 'center',
                        formatter:function (value) {
                            if (value == 1)
                                return '可操作';
                            if (value == 2)
                                return '已还款';
                            if(value== 3)
                                return '已变现';
                            if(value== 4)
                                return '已使用';
                        }}]],
                onLoadSuccess:function(){
                    xyz.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onClickRow:function(){
                    xyz.setBtnStatus();
                },
                onCheck :function(){
                    xyz.setBtnStatus();
                }
            });
        }
    }});
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_xyz_add").click(function () {
        xyz_add('新增信用证', 'add_xyz');
    });

    //查询按钮
    $("#btn_xyz_search").click(function () {
        xyz_search();
    });

    //信用证还款
    $("#btn_xyz_xyzhk").click(function () {
        xyz_hk('信用证还款');
    });

    //信用证还款
    $("#btn_xyz_xyzbx").click(function () {
        xyz_bx('信用证变现');
    });

    //修改信用证
    $("#btn_xyz_mod").click(function () {
        xyz_modefy('修改信用证', 'update_xyz');
    });

    $("#btn_xyz_del").click(function () {
        xyz_delete();
    });

    $("#xyz_zt").combobox({
        data:[{
            label: '可操作',
            value: '1'
        },{
            label: '已变现',
            value: '3'
        }],
        onLoadSuccess:function(){
            $("#xyz_zt").combobox('setValue',1);
        }
    });



});
//信用证添加
function xyz_add(title, action) {
    var href = $.webPath() + "/zjgl/xyz/xyz_content";
    var actionUrl = $.webPath() + "/zjgl/xyz/add_xyz";
    xyz_showDialog(title, href, actionUrl);
}

//修改信用证
function xyz_modefy(title, action) {
    var rows = $("#grid_xyz").datagrid("getSelections");
    if(rows.length>0){
        if(rows[0].status != 1){
            $.messager.alert("提示", "请选择可操作的信用证!", "warning");
            return;
        }
        if (rows.length>1) {
            $.messager.alert("提示", "一次只能更新一个记录!", "warning");
            return;
        }
        var href = $.webPath() + "/zjgl/xyz/xyz_content?xyzId="+rows[0].id;
        var actionUrl = $.webPath() + "/zjgl/xyz/update_xyz";
        xyz_showDialog(title, href, actionUrl);
    }else{
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
}

//删除信用证
function xyz_delete() {
    var rows = $("#grid_xyz").datagrid("getSelections");
   if(rows.length >1){
       $.messager.alert("提示", "一次只可删除一条数据！", "warning");
       return ;
   }
    if(rows[0].status !=1){
        $.messager.alert("提示", "请删除可操作信用证！", "warning");
        return ;
    }
    if (rows) {
        $.messager.confirm('提示', '是否删除该信用证？', function (r) {
            if (r) {
                var index = $('#grid_xyz').datagrid('getRowIndex', rows[0]);
                $.ajax({
                    url: $.webPath() + "/zjgl/xyz/delete_xyz",
                    dataType: 'json',
                    type: 'POST',
                    data: {xyzId:rows[0].id},
                    beforeSend: function () {
                        $("#layout_xyz").mask();
                    },
                    success:function(data){
                        $("#layout_xyz").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_xyz').datagrid('deleteRow', index);
                            $('#grid_xyz').datagrid('reload');

                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的信用证！", "warning");
    }
}

//信用证还款
function xyz_hk(title, action) {
    var rows = $("#grid_xyz").datagrid("getSelections");
    var id = rows[0].nbgs_jc;
    var bankid = rows[0].yfczyh_qc;
    var xyzIds = "";
    for(var i=0;i<rows.length;i++){
        if(id != rows[i].nbgs_jc){
            $.messager.alert("提示", "请选择同一家公司信用证还款!", "warning");
            return;
        }
        if(rows[i].status != 4  ){
            $.messager.alert("提示", "请选择已使用的信用证!", "warning");
            return;
        }
        if(rows[i].sfdrlx == 'wdr'  ){
            $.messager.alert("提示", "请选择已导入利息的信用证!", "warning");
            return;
        }
        if(bankid != rows[i].yfczyh_qc){
            $.messager.alert("提示", "请选择同一家银行信用证还款!", "warning");
            return;
        }
        xyzIds += rows[i].id+",";
    }
    xyzIds=xyzIds.substring(0,xyzIds.length-1);
    var href = $.webPath() + "/zjgl/xyz/xyz_hk?xyzIds="+xyzIds;
    var actionUrl = $.webPath() + "/zjgl/xyz/hk_xyz";
    xyz_hk_showDialog(title, href, actionUrl);
}

//信用证变现
function xyz_bx(title, action) {
    var rows = $("#grid_xyz").datagrid("getSelections");
    var id = rows[0].nbgs_jc;
    var bankid = rows[0].ysczyh;
    var xyzIds = "";
    for(var i=0;i<rows.length;i++){
        if(id != rows[i].nbgs_jc){
            $.messager.alert("提示", "请选择同一家公司信用证变现!", "warning");
            return;
        }
        if(rows[i].status != 1){
            $.messager.alert("提示", "请选择可操作的信用证!", "warning");
            return;
        }
        if(bankid != rows[i].ysczyh){
            $.messager.alert("提示", "请选择同一家银行信用证变现!", "warning");
            return;
        }
        xyzIds += rows[i].id+",";
    }
    xyzIds=xyzIds.substring(0,xyzIds.length-1);
    var href = $.webPath() + "/zjgl/xyz/xyz_bx?xyzIds="+xyzIds;
    var actionUrl = $.webPath() + "/zjgl/xyz/bx_xyz";
    xyz_bx_showDialog(title, href, actionUrl);
}


//弹出新增或修改的窗口
function xyz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_xyz').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_xyz').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出新增或修改的窗口
function xyz_hk_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_xyz_hk').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_xyz').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出新增或修改的窗口
function xyz_bx_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_xyz_bx').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_xyz').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//查询票据
function xyz_search() {
    var gsMc =  $("#xyzgsId").combotree('getText') == null ? null :  $("#xyzgsId").combotree('getText');
    var gsId =  $("#xyzgsId").combotree('getValue') == null ? null :  $("#xyzgsId").combotree('getValue');
    var khhMc =  $("#xyzkhhId").combotree('getText') == null ? null :  $("#xyzkhhId").combotree('getText');
    var khhId =  $("#xyzkhhId").combotree('getValue') == null ? null :  $("#xyzkhhId").combotree('getValue');
    var zt = $('#xyz_zt').combobox('getValue') == null ? null : $('#xyz_zt').combobox('getValue');
    $('#grid_xyz').datagrid('load',
        {
            xyzsflxType: $("[name='xyzsflx_type']").val(),
            shztType: $("[name='shzt_type']").val(),
            gsId: gsId ,
            gsMc: gsMc,
            khhMc: khhMc ,
            khhId: khhId,
            xyzhm: $("#xyz_xyzhm").val(),
            zt:zt
        }
    );
}
